package com.jacobson.huobi.mvp

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.jacobson.core.base.Immersive
import com.jacobson.huobi.R
import kotlinx.android.synthetic.main.activity_help_web_view.*

@Immersive(fitSystemWindows = false)
class HelpWebViewActivity : Activity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_help_web_view)
        mHelpWebview.getSettings().setJavaScriptEnabled(true)
        val settings = mHelpWebview.settings
        settings.javaScriptEnabled = true
        settings.setAppCacheEnabled(true)
        settings.cacheMode = WebSettings.LOAD_DEFAULT
        settings.setAppCachePath(cacheDir.path)
        settings.setSupportZoom(false)
        settings.builtInZoomControls = false
        settings.displayZoomControls = false
        settings.blockNetworkImage = false
        settings.loadsImagesAutomatically = true
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            settings.safeBrowsingEnabled = true
        }
        settings.useWideViewPort = true
        settings.loadWithOverviewMode = true
        settings.javaScriptCanOpenWindowsAutomatically = true
        settings.domStorageEnabled = true
        settings.setSupportMultipleWindows(true)
        settings.loadWithOverviewMode = true
        settings.setGeolocationEnabled(true)
        settings.allowFileAccess = true
        mHelpWebview.fitsSystemWindows = true
        mHelpWebview.setLayerType(View.LAYER_TYPE_HARDWARE, null)
        mHelpWebview.loadUrl("http://www.celueren.com/index.html")
        mHelpWebview.webViewClient = object : WebViewClient() {
            override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                super.onPageStarted(view, url, favicon)

            }

            override fun onPageFinished(view: WebView?, url: String?) {
                super.onPageFinished(view, url)

            }
        }
        btnBack.setOnClickListener {
            finish()
        }
    }
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && event?.action==KeyEvent.ACTION_DOWN){

        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onBackPressed() {
        super.onBackPressed()
        if (mHelpWebview.canGoBack()) {
            // If web view have back history, then go to the web view back history
            mHelpWebview.goBack()
        }
    }
    companion object {
        fun startActivity(context: Context) {
            Intent(context, HelpWebViewActivity::class.java).run {
                context.startActivity(this)
            }
        }
    }
}
